package avicit.bdp.dms.das.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.common.utils.BdpLogUtil;
import avicit.bdp.dms.das.dao.BdpDasBusinessLinkDao;
import avicit.bdp.dms.das.dto.BdpDasBusinessLinkDTO;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.core.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2023-09-06 14:54
 * @类说明：bdp_das_businesslinkService
 * @修改记录：
 */
@Service
public class BdpDasBusinessLinkService extends BaseService<BdpDasBusinessLinkDao, BdpDasBusinessLinkDTO> {

    private static final Logger LOGGER = LoggerFactory.getLogger(BdpDasBusinessLinkService.class);

   

    /**
     * 新增对象
     *
     * @param bdpDasBusinessLinkDTO 保存对象
     * @return String
     */
    @Transactional
    public BdpDasBusinessLinkDTO insertBdpDasBusinessLink(BdpDasBusinessLinkDTO bdpDasBusinessLinkDTO) {
        try {
            bdpDasBusinessLinkDTO.setId(ComUtil.getId());
            insertSelective(bdpDasBusinessLinkDTO);
            //记录日志
            BdpLogUtil.log4Insert(bdpDasBusinessLinkDTO);
            return bdpDasBusinessLinkDTO;
        } catch (Exception e) {
            throw new BusinessException(e.getMessage(), e);
        }
    }


    /**
     * 按主键删除
     *
     * @param id
     */
    @Transactional
    public void deleteBusinessLink(String id) {
        if(StringUtils.isBlank(id)){
            throw new BusinessException("参数为空！");
        }
            try {
                //记录日志
                BdpDasBusinessLinkDTO bdpDasBusinessLinkDTO = selectByPrimaryKey(id);
                if (bdpDasBusinessLinkDTO == null) {
                    throw new BusinessException("删除失败！对象不存在");
                }
                BdpLogUtil.log4Delete(bdpDasBusinessLinkDTO);
                //删除业务数据
                deleteByPrimaryKey(id);
            } catch (Exception e) {
                throw new BusinessException(e.getMessage(), e);
            }
    }


    /**
     * 按模型ID删除线关系
     *
     * @param businessId
     */
    public void deleteLinkByBusinessId(String businessId) {
    	this.mapper.deleteLinkByBusinessId(businessId);
    }



    public  List<BdpDasBusinessLinkDTO> searchLinkByBusinessId(List<String> businessIds){
        return  this.mapper.searchLinkByBusinessId(businessIds);
    }

    public void updateBdpDasBusinessLink(BdpDasBusinessLinkDTO bdpDasBusinessLink) {
        try {
            updateByPrimaryKey(getBdpDasBusinessLinkDTO(bdpDasBusinessLink));
        } catch (Exception e) {
            throw new BusinessException(e.getMessage(), e);
        }
    }

    private BdpDasBusinessLinkDTO getBdpDasBusinessLinkDTO(BdpDasBusinessLinkDTO bdpDasBusinessLink) {
        BdpDasBusinessLinkDTO oldDTO = selectByPrimaryKey(bdpDasBusinessLink.getId());
        if (oldDTO == null) {
            throw new BusinessException("数据不存在");
        }
        // 记录日志
        BdpLogUtil.log4Update(bdpDasBusinessLink, oldDTO);

        oldDTO.setName(bdpDasBusinessLink.getName());
        return oldDTO;
    }
}